package com.slunk.pavornocturnus;

//import java.lang.reflect.Constructor;
//import java.lang.reflect.InvocationTargetException;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
//import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.slunk.accessors.SDialogueAccessor;
import com.slunk.accessors.SRenderSpriteAccessor;
import com.slunk.accessors.SpriteAccessor;
//import com.slunk.interfaces.HotSpotInterface;
import com.slunk.pavornocturnus.PavorNocturnusGameObject;
import com.slunk.platforms.SDialogue;
import com.slunk.platforms.SRenderSprite;
import com.slunk.platforms.SSpriteCollection;
import com.slunk.platforms.STouchHandler;
import com.slunk.platforms.STouchHandlerEvents;
//import com.slunk.pavornocturnus.screens.PavorNocturnusLeftPanelScreen;
//import com.slunk.pavornocturnus.screens.*;

public abstract class PavorNocturnusScreenObject implements Screen, STouchHandlerEvents, InputProcessor {
	private static final int VIRTUAL_WIDTH = 1280;
	private static final int VIRTUAL_HEIGHT = 780;
	private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH / (float)VIRTUAL_HEIGHT;
	private Rectangle viewport;
	protected final OrthographicCamera camera;
	protected final PavorNocturnusGameObject game;
    protected final SpriteBatch batch;
    protected final SSpriteCollection spriteCollection;
    protected final TweenManager manager;
    protected final SDialogue testDialogue;
	protected final STouchHandler newHandler;
	protected final TextureAtlas textureAtlas;
	//protected final  InputMultiplexer inputMultiplexer;
	
    public PavorNocturnusScreenObject(PavorNocturnusGameObject game) {
    	this.game = game;
    	batch = new SpriteBatch();
    	
    	camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.position.set(new Vector3(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,0));
    	//camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
		//camera.position.set(new Vector3(VIRTUAL_WIDTH/2,VIRTUAL_HEIGHT/2,0));
		
    	textureAtlas = new TextureAtlas(Gdx.files.internal("packs/texture.atlas"));
		spriteCollection = new SSpriteCollection();
		manager = new TweenManager();
		this.newHandler = new STouchHandler(this,camera);
		testDialogue = new SDialogue(textureAtlas.createPatch("dialoguePatch"),Gdx.graphics.getWidth(),50);
		
		//Register the tween accessor
		registerAccessors();
		
		//do not enforce power of two images
		Texture.setEnforcePotImages(false);
    }
    
    public void registerAccessors() {
    	Tween.registerAccessor(Sprite.class, new SpriteAccessor());
		Tween.registerAccessor(SDialogue.class, new SDialogueAccessor());
		Tween.registerAccessor(SRenderSprite.class, new SRenderSpriteAccessor());
    }
    
    @Override
    public void show()
    {
    	
    }

    @Override
    public void resize(int width,int height)
    {
    	/*
    	float aspectRatio = (float)width/(float)height;
    	float scale = 1f;
    	Vector2 crop = new Vector2(0f,0f);
    	
    	if (aspectRatio > ASPECT_RATIO) {
    		scale = (float)height/(float)VIRTUAL_HEIGHT;
    		crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
    	} else if (aspectRatio < ASPECT_RATIO) {
    		scale = (float)width/(float)VIRTUAL_WIDTH;
    		crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
    	} else {
    		scale = (float)width/(float)VIRTUAL_WIDTH;
    	}
    	
    	float w = (float)VIRTUAL_WIDTH*scale;
    	float h = (float)VIRTUAL_HEIGHT*scale;
    	viewport = new Rectangle(crop.x,crop.y,w,h);
    	*/
    }

    @Override
    public void render(float delta)
    {
        // the following code clears the screen with the given RGB color (black)
        Gdx.gl.glClearColor( 1f, 1f, 1f, 1f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
        
        //refresh the camera
        camera.update();
        //camera.apply(Gdx.gl10);
        
        //Gdx.gl.glViewport((int)viewport.x, (int)viewport.y, (int)viewport.width, (int)viewport.height);
        
		batch.setProjectionMatrix(camera.combined);
		
		//draw all the sprites
		batch.begin();
			spriteCollection.drawAll(batch);
		batch.end();
		
		//update the tween manager
		manager.update(delta);
    }

    @Override
    public void hide()
    {
    }

    @Override
    public void pause()
    {
    }

    @Override
    public void resume()
    {
    }

    @Override
    public void dispose()
    {
        batch.dispose();
    }
    
    public void onHotSpotTouched(PavorNocturnusHotSpots hotSpot)  {
    	this.game.setPavorNocturnusScreen(hotSpot.fullyQualifiedClassName);
    }
}
